package net.chathu.airgw.packets.chat 
{
	import air.update.utils.StringUtils;
	import net.chathu.airgw.Debug;
	import net.chathu.airgw.model.JoinedRoomInfo;
	import net.chathu.airgw.model.Room;
	import net.chathu.airgw.util.StringUtil;

	/**
	 * ...
	 * @author ze
	 */
	public class ItemsRooms extends ChatPacket
	{
		
		public function ItemsRooms() 
		{
			
		}
		
		override public function execute():void
		{
			if (data[1] == "rooms")
			{
				/*
				 * Ez akkor fut le, ha a szobak listajat kuldte a szerver
				 */
				var j:int=2;
				while (j + 2 < data.length)
				{
					try {
						var type:String = String(data[j + 14]);
						if( type.indexOf("folder") ==0 ) {
							//ez egy folder, nem kell csatornat letrehozni
						} else {
							var pos:int = data[j+8].indexOf(":");
							var channel_id:int,invite_id:int,usernum:int,modnum:int;
							var channel_name:String, topic:String;
							channel_id=parseInt(data[j+1]);
							invite_id=0;
							channel_name = data[j + 14].substring(5);
							if (channel_name == "ausztraila")
							{
								//az Ausztrália szoba neve el van írva: ausztraILa
								channel_name = "ausztralia";
							}
							topic=StringUtil.convertFromISO88591(data[j+18]);
							
							usernum=parseInt(data[j+8].substring(0,pos));
							modnum=parseInt(data[j+8].substring(pos+1));
							var room:Room = new Room(channel_id, invite_id, channel_name, topic, usernum, modnum, false, false);
							sessionData.chatData.rooms.add(room);
						}
					}catch(err:Error) {
						trace(err.message);
					}
					j+=22;
				}
				//trace("#Num. of rooms:" +sessionData.chatData.rooms.getRoomNum());
			} else {
				/*
				 * Ha a rooms-on kivul van meg tovabbi szoveg is a 2. elemben,
				 * pl: items|rooms/38/users ...
				 * 
				 * vagy:->beleptel egy szobaba, ime a felhasznalok:
				 * items|rooms/136/users|9|81690|1|id|81690|394567|1|id|394567|557108|1|id|557108|557192|1|id|557192|557598|1|id|557598|557630|1|id|557630|583664|1|id|583664|605213|1|id|605213|725996|1|id|725996
				 * 
				 * Ez akkor fut le, ha beleptunk szobaba, es van ott felhasznalo...igy kuldi el az userek neveit...
				 * Ha nincs a szobaban user, akkor ez (valoszinuleg) nem fut le!
				 * 
				 * Szobaba belepesnel a kovetkezok jonnek: (items|rooms/..) nem feltetlenul jon!
				 * >> sub|rooms/136/users 
				 * >> items|rooms/136/users|9|81690|1|id|81690|394567|1|id|394567|557108|1|id|557108|557192|1|id|557192|557598|1|id|557598|557630|1|id|557630|583664|1|id|583664|605213|1|id|605213|725996|1|id|725996
				 * >> iadd|rooms/136/users|726004|1|id|726004
				 * >> sub|rooms/136/chat
				 */
				
				var posA:int=(data[1]).indexOf('/',0) +1;
				var posB:int = (data[1]).indexOf('/', 7) ;
				var secondword:String = (data[1]).substring(posB + 1);
			
				if( secondword == "users" ) {
    	    	
			
					var roomId:int=parseInt(data[1].substring(posA, posB));
		
					var joinedRoomInfo:JoinedRoomInfo = sessionData.chatData.getJoinedRoomInfo(roomId);
					if (joinedRoomInfo)
					{
						trace("Shouldn't happen. | Dupla belepes egy szobaba(?)");
						throw new Error("Duplicated join to a room?! (ItemsRooms)");
					} else {
						joinedRoomInfo = new JoinedRoomInfo(roomId);
						
						var i:int=3;
						while (i + 2 < data.length) {
							var rights:int=0;
							if (data[i + 2] == "r") { //rights
								rights=parseInt(data[i+3]); //jogcsoport(2=kiemelt)
							}
							
							var userId:int=parseInt(data[i]);
							joinedRoomInfo.addUser(userId, rights);
							i+=4;
						}
						
						sessionData.chatData.joinedRooms.push(joinedRoomInfo);
					}
				}
			}
		}
	}
}